package com.forlove.app.qselect.friend.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.NestedScrollView;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.forlove.app.R;
import com.forlove.app.common.base.ui.BaseActivity;
import com.forlove.app.common.widget.recyview.BaseRecyclerAdapter;
import com.forlove.app.manager.ContactsManager;
import com.forlove.app.manager.FriendManager;
import com.forlove.app.manager.NetworkManager;
import com.forlove.app.qselect.CommonEntity;
import com.forlove.app.qselect.friend.adapter.AddNewFriendAdapter;
import com.forlove.app.qselect.friend.entity.MineFriendEntity;
import com.forlove.app.qselect.friend.entity.NewsFriendsStatus;
import com.forlove.app.qselect.friend.entity.SearchFriendEntity;
import com.forlove.app.qselect.friend.presenter.NewFriendPresenter;
import com.forlove.app.qselect.friend.view.NewFriendView;
import com.forlove.app.qselect.index.ui.MineInfoActivity;
import com.forlove.app.qselect.login.entity.ContactsInfoEntity;
import com.forlove.app.qselect.login.widget.ResetPwTipDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * 新的好友页面
 * Created by yjl on 2017/10/14.
 */

public class NewFriendActivity extends BaseActivity implements
        View.OnClickListener, NewFriendView,
        SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.view_fade_back)
    FrameLayout view_fade_back;
    @BindView(R.id.view_friend)
    NestedScrollView view_friend;

    @BindView(R.id.list_new_friends)
    RecyclerView list_new_friends;

    @BindView(R.id.view_search_account)
    RelativeLayout view_search_account;

    @BindView(R.id.text_add_phone_friend)
    AppCompatTextView text_add_phone_friend;

    @BindView(R.id.text_add_new_friend)
    TextView text_add_new_friend;

    @BindView(R.id.refresh_new_friend)
    SwipeRefreshLayout refresh_new_friend;

    private AddNewFriendAdapter addNewFriendAdapter;
    private ArrayList<MineFriendEntity> friendEntities = new ArrayList<>();
    private NewFriendPresenter newFriendPresenter;
    private ResetPwTipDialog resetPwTipDialog;

    @Override
    public int getLayoutId() {
        return R.layout.activity_new_firends;
    }

    @Override
    public void initView() {
        newFriendPresenter = new NewFriendPresenter(this);
        setPresenter(newFriendPresenter);
        initViewData();
        requestNewFriend();
    }

    private void initViewData() {
        text_add_new_friend.setOnClickListener(this);
        view_search_account.setOnClickListener(this);
        text_add_phone_friend.setOnClickListener(this);
        view_fade_back.setOnClickListener(this);
        refresh_new_friend.setOnRefreshListener(this);
        refresh_new_friend.setColorSchemeResources(R.color.common_green);
//        if (FriendManager.getNewFriends() != null) {
//            friendEntities.clear();
//            friendEntities.addAll(FriendManager.getNewFriends());
//        }
        addNewFriendAdapter = new AddNewFriendAdapter(this,
                R.layout.item_new_friends, friendEntities, newFriendPresenter);
        addNewFriendAdapter.hideFooterView();
        addNewFriendAdapter.hideEmptyView();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        list_new_friends.setLayoutManager(linearLayoutManager);
        list_new_friends.setAdapter(addNewFriendAdapter);
        addNewFriendAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                if (position < friendEntities.size()) {
                    MineFriendEntity friendEntity = friendEntities.get(position);
                    //添加好友，好友申请页面
                    if (friendEntity.status == NewsFriendsStatus.UNCONFIRM.getCode()
                            || friendEntity.status == NewsFriendsStatus.UNADDED.getCode()) {
                        SearchFriendEntity searchFriendEntity = new SearchFriendEntity();
                        searchFriendEntity.friendEntity = friendEntity;
                        searchFriendEntity.isFriend = false;
                        searchFriendEntity.note_times = friendEntity.charm;
                        Intent intent = new Intent(NewFriendActivity.this, SearchUserInfoActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("friendinfo", searchFriendEntity);
                        bundle.putString("mobile", friendEntity.getAvaliableMobile());
                        intent.putExtras(bundle);
                        startActivity(intent);
                    } else if (friendEntity.status == NewsFriendsStatus.CONFIRMED.getCode()) {
                        Intent intent = new Intent(NewFriendActivity.this, MineInfoActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putString("mobile", friendEntity.mobile);
                        intent.putExtras(bundle);
                        startActivity(intent);
                    }
                }
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, final int position) {
                if (resetPwTipDialog == null) {
                    resetPwTipDialog = new ResetPwTipDialog(NewFriendActivity.this);
                }
                resetPwTipDialog.setTextMesseage("确定删除该好友记录么？");
                resetPwTipDialog.setAgreeListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (position < friendEntities.size()) {
                            MineFriendEntity friendEntity = friendEntities.get(position);
                            requestDeleteFriendRecord(friendEntity.mobile);
                        }
                        resetPwTipDialog.dismiss();
                    }
                });
                resetPwTipDialog.show();
                return false;
            }
        });

    }

    private void requestDeleteFriendRecord(String mobile) {
        Map param = new HashMap();
        param.put("mobile", mobile);
        newFriendPresenter.loadDeleteFriendRecord(param);
    }

    private void requestNewFriend() {
        Map param = new HashMap();
        newFriendPresenter.loadNewFriendData(param);
    }

    @Override
    protected void onResume() {
        super.onResume();
//        StatusBarCompat.compat(this, Color.parseColor("#ffffff"));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.view_search_account:
                // 此处跳转需要讨论
                startActivity(new Intent(this, SearchFriendActivity.class));
                break;
            case R.id.text_add_phone_friend:
                //跳转添加手机联系人
                startActivity(new Intent(this, AddContactsFriendActivity.class));
                break;
            case R.id.view_fade_back:
                onBackPressed();
                break;
            case R.id.text_add_new_friend:
                startActivity(new Intent(this, AddNewFriendActivity.class));
                break;

        }
    }

    private List<MineFriendEntity> copyContactsFriend(List<MineFriendEntity> friendEntityList) {
        List<ContactsInfoEntity> contactsInfoEntities = ContactsManager.getContectInfo(this);
        List<MineFriendEntity> entities = FriendManager.contactsToFriendEntity(friendEntityList, contactsInfoEntities);
        return entities;
    }

    @Override
    public void onRefresh() {
        requestNewFriend();
        if (mHandler == null) {
            mHandler = new Handler(getMainLooper());
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    refresh_new_friend.setRefreshing(false);
                }
            }, 6000);
        }
    }

    @Override
    public void showErrorData(NetworkManager.ResultCode resultCode, String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
        refresh_new_friend.setRefreshing(false);
    }

    @Override
    public void showNewFriendData(List<MineFriendEntity> mineFriend) {
        refresh_new_friend.setRefreshing(false);
        if (mineFriend != null) {
            List<MineFriendEntity> entities = new ArrayList<>();
            entities.addAll(copyContactsFriend(mineFriend));
            for (MineFriendEntity mineFriendEntity : mineFriend) {
                //TODO 移除好友请求信息
                if (mineFriendEntity.status == NewsFriendsStatus.UNCONFIRM.getCode()) {
                    entities.remove(mineFriendEntity);
                }
            }
            if (entities.size() > 0) {
                friendEntities.clear();
                friendEntities.addAll(FriendManager.sortNewFriend(entities));
            }
            addNewFriendAdapter.updateDatas(friendEntities);
        }
    }

    @Override
    public void showAgreeFriend(NetworkManager.ResultCode resultCode, String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
        if (resultCode.equals(NetworkManager.ResultCode.RESULT_SUCCESS)) {
            requestNewFriend();
        }
    }

    @Override
    public void showDeleteRecordView(CommonEntity commonEntity) {
        Toast.makeText(this, "删除成功！", Toast.LENGTH_SHORT).show();
        requestNewFriend();
    }
}
